<?php
mb_internal_encoding ( "UTF-8" );
header('Content-Type: text/html; charset=UTF-8');
include 'util.php';
session_start ();

$arrayPalabras = array (
	0 => "Programación",
	1 => "Javascript",
	2 => "Paragüas",
	3 => "España"
	);

if (isset ( $_SESSION ['antes'] )) {
	$palabra = $_SESSION ['palabra'];
} else {
	$_SESSION ['antes'] = true;
	$_SESSION ['booleano'] = array ();
	$_SESSION ['fallos'] = 0;
	$_SESSION ['letrasIntentadas'] = array ();
	
	$aleatorio = rand ( 0, (count ( $arrayPalabras ) - 1) );
	$palabra = $arrayPalabras [$aleatorio];
	$_SESSION ['palabra'] = $palabra;
}

if (isset ( $_REQUEST ['cambiar'] )) {
	$_SESSION ['antes'] = true;
	$_SESSION ['booleano'] = array ();
	$_SESSION ['fallos'] = 0;
	$_SESSION ['letrasIntentadas'] = array ();
	
	$aleatorio = rand ( 0, (count ( $arrayPalabras ) - 1) );
	$_REQUEST = null;
	$palabra = $arrayPalabras [$aleatorio];
	$_SESSION ['palabra'] = $palabra;
}

$longitud = mb_strlen( $palabra );

?>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Ahorcado</title>
	<style>
	body {
		margin: 35px;
	}

	.l {
		width: 40px;
		height: 40px;
		font-size: 40px;
		margin: 2px;
		text-align: center;
	}
	
	#contenedor {
		display: inline-block;
		border-style: double;
		border-color: black;
		border-width: 4px;
		padding: 3px;
	}
	
	.cuadro {
		width: 500px;
		height: 200px;
		border: solid 2px black;
		padding: 7px;
	}

	.prueba {
		width: 30px;
		height: 30px;
		font-size: 30px;
		padding: 5px;
		margin-left: 10px;
	}

	.boton {
		margin-right: 20px;
	}
	</style>
</head>
<body>
	<form name="formulario" method="post">
		<div id="contenedor">
<?php
		
		$casillasBien = 0;
		$leido = isset ( $_REQUEST ['prueba'] ) ? $_REQUEST ['prueba'] : "-";
		$leido = mb_strtolower(filtro($leido),'UTF-8');
		
		$existeLetra = false;
		
		
		if (mb_strlen($leido)>1) {
			$leido = "-";
		}
		
		for ($i = 0; $i < $longitud; $i++) {
			$letra = strtolower(filtro(mb_substr($palabra,$i,1)));
			if ($letra == $leido) {
				$existeLetra = true;
				break;
			}
		}
		
		if ($leido != "-" && $leido != "") {
			
			if (! isset ( $_SESSION['letrasIntentadas'][$leido] )) {
				$_SESSION['letrasIntentadas'] [$leido] = $leido;
				if (! $existeLetra) {
					$_SESSION ['fallos']++;
				}
			}
		}
		
		for($i = 0; $i < $longitud; $i ++) {
			$pintar = "-";
			
			if ($leido == "") {
				$leido = "-";
			}
			
			if (strtolower(filtro(mb_substr($palabra,$i,1))) == $leido) {
				$pintar = mb_substr($palabra,$i,1);
				$_SESSION ['booleano'] [$i] = true;
				$casillasBien ++;
			} else if (isset ( $_SESSION ['booleano'] [$i] )) {
				$pintar = mb_substr($palabra,$i,1);
				$casillasBien ++;
			}
			
			echo "		<input class=\"l\" type=\"text\" disabled value=\"$pintar\">\r\n";
		}
		
		$fallos = $_SESSION ['fallos'];
		
		?>
		</div>
		
		<br /> </br>
		
		<label for="introduce" class="introduce">Introduce una	letra</label>
		<input class="prueba" type="text" autofocus name="prueba" maxlength="1" <?php if ($fallos >= 6 || $casillasBien == $longitud) {echo "disabled";}?> >
		<br /> <br />
		<input type="submit" value="Comprobar" class="boton">
		<input type="submit" value="Generar palabra aleatoria" class="boton" name="cambiar" />
		<br /> <br />
		<div class="cuadro">

			<?php			
			$intentos = $_SESSION ['letrasIntentadas'];
			$lp = aToString($intentos);
			$intentos = count ( $intentos );
			
			
			if ($casillasBien == $longitud) {
				session_destroy ();
				$_REQUEST ['cambiar'] = true;
				echo "Enhorabuena, has acertado todas\r\n";
				echo "<br/ >Lo has conseguido tras $fallos fallos y $intentos intentos\r\n";
			} else if ($intentos == 0) {
				session_destroy ();
				echo "Se ha generado otra palabra, vuelve a jugar\r\n";
				$_SESSION ['fallos'] = 0;
			} else if ($fallos >= 6) {
				session_destroy ();
				$_REQUEST ['cambiar'] = true;
 				echo "Lo sentimos, has cometido 6 fallos en $intentos intentos";
 				echo "\r\n<br />La palabra era $palabra";
			} else {
				echo "<b>Letras probadas:</b>$lp\r\n<br />";
				echo "<b>Número de intentos:</b> $intentos\r\n<br />";
				echo "<b>Número de fallos:</b> $fallos (6)\r\n<br />";
			}
			
			?>
		</div>

	</form>
</body>
</html>